# -*- coding: utf-8 -*-
from django.shortcuts import redirect
from bank.app.models import Account, Payment, Card
from django.db.models import Q

error_messages = {'noAccount' : 'No Account found',
                'missingArg' : 'Missing arguments',
                'missingCommand' : 'Missing command',
                'unknownCommand' : 'Unknown command',
                'noBack' : 'No account to go back',
                'noCard' : 'No Card found',
                'noPayment' : 'No Payment found',
}

def accounts(request, context):
    return redirect('accounts')

def account(request, context):
    arg_arr = context['arg_arr']
    
    if len(arg_arr) == 1:
        account = get_or_none(Account, user=request.user, id=arg_arr[0])
        if account:
            return redirect('account_details', account_id=account.id)
        response = redirect('cli_home')
        response['Location'] += '?error=noAccount'
    else:
        response = redirect('cli_home')
        response['Location'] += '?error=missingArg'
    return response

def back(request, context):
    account_id = False
    payment_id = False
    card_id = False
    
    if 'account_id' in request.POST and request.POST['account_id']:
        #return redirect('account_details', account_id=request.POST['account_id'])
        account_id = True
    if 'payment_id' in request.POST and request.POST['payment_id']:
        #return redirect('account_details', payment_id=request.POST['payment_id'])
        payment_id = True
    if 'card_id' in request.POST and request.POST['card_id']:
        #return redirect('account_details', card_id=request.POST['card_id'])
        card_id = True
        
    if account_id:
        if payment_id:
            return redirect('list_account_payments', account_id=request.POST['account_id'])
        if card_id:
            return redirect('list_account_cards', account_id=request.POST['account_id'])
            
        
        return redirect('account_details', account_id=request.POST['account_id'])
        
    if payment_id:
        return redirect('list_payments')
    if card_id:
        return redirect('list_cards')

    return redirect('accounts')


def account_history(request, context):
    if 'account_id' in request.POST and request.POST['account_id']:
        return redirect('list_account_payments', account_id=request.POST['account_id'])
    return redirect('list_payments')

def transfer(request, context):
    return redirect('transfer')
    
def new_payment(request, context):
    if 'account_id' in request.POST and request.POST['account_id']:
        return redirect('account_new_payment', account_id=request.POST['account_id'])
    return redirect('new_payment')
    
def cards(request, context):
    if 'account_id' in request.POST and request.POST['account_id']:
        return redirect('list_account_cards', account_id=request.POST['account_id'])
    return redirect('list_cards')
    
def card_detail_cli(request, context):
    arg_arr = context['arg_arr']
    
    if len(arg_arr) == 1:
        card = get_or_none(Card, account__user=request.user, id=arg_arr[0])
        if card:
            if 'account_id' in request.POST and request.POST['account_id']:
                return redirect('account_card_details', account_id=request.POST['account_id'], card_id=card.id)
            return redirect('card_details', card_id=card.id)
        response = redirect('cli_home')
        response['Location'] += '?error=noCard'
    else:
        response = redirect('cli_home')
        response['Location'] += '?error=missingArg'
    return response
    
def cli_payment_details(request, context):
    arg_arr = context['arg_arr']
    
    if len(arg_arr) == 1:

        try:
            payment = Payment.objects.get(Q(to_account__user=request.user) | Q(from_account__user=request.user), id=arg_arr[0])
        except Payment.DoesNotExist:
            payment = None

        if payment:
            if 'account_id' in request.POST and request.POST['account_id']:
                return redirect('account_payment_details', account_id=request.POST['account_id'], payment_id=payment.id)
            return redirect('payment_details', payment_id=payment.id)
        response = redirect('cli_home')
        response['Location'] += '?error=noPayment'
    else:
        response = redirect('cli_home')
        response['Location'] += '?error=missingArg'
    return response
    
def cli_login(request, context):
    return redirect('type_app_login', login_type='cli' )
    
def cli_logout(request, context):
    return redirect('app_logout')

# Order of functions and dictionary is vital:

cli_commands = {'home' : accounts,
                'account' : account,
                'transfer' : transfer,
                'accounts' : accounts,
                'newpayment' : new_payment,
                'cards' : cards,
                'history' : account_history,
                'payment' : cli_payment_details,
                'back' : back,
                'card' : card_detail_cli,
                'logout' : cli_logout,
                'login' : cli_login,
}
'''
cli_commands = {'koti' : accounts,
                'tili' : account,
                'siirto' : transfer,
                'tilit' : accounts,
                'uusimaksu' : new_payment,
                'kortit' : cards,
                'historia' : account_history,
                'takaisin' : back_to_account,
                'kortti' : card_detail_cli,
                'ulos' : cli_logout,
                'sisaan' : cli_login,
}
'''

def direct_command(request, context = {}):

    arg_arr = []
    command_arr = context['command_arr']
    
    if command_arr[0] in cli_commands:
        #command = cli_commands[command_arr[0]]
        
        if len(command_arr) > 1:
            arg_arr = command_arr[1:]
        
        context['arg_arr'] = arg_arr
        return cli_commands[command_arr[0]](request, context)
        
    response = redirect('cli_home')
    response['Location'] += '?error=unknownCommand'
    return response

def get_or_none(model, **kwargs):
    try:
        return model.objects.get(**kwargs)
    except model.DoesNotExist:
        return None
    
